package org.example.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;

/**
 * @author 莫磊
 * @date 2025/8/10
 * @description
 */
@Controller
@Slf4j
public class RESTFulController {

	@GetMapping("/user/{id}")
	public String getUserById(@PathVariable("id") String id) {
		System.out.println("获取用户id为：" + id + "的数据");
		return "index";
	}

	@GetMapping("/user")
	public String getAllUser() {
		System.out.println("获取所有用户数据");
		return "index";
	}

	@PostMapping("/user")
	public String addUser(User user) {
		System.out.println("添加用户数据");
		return "index";
	}

	@PostMapping("/user/forString")
	public String addUserForString(@RequestBody User user) {
		System.out.println("添加用户数据");
		return "index";
	}

	@PutMapping("/user")
	public String updateUser(HttpServletRequest request, User user) {
		System.out.println("更新用户数据: " + user);
		System.out.println("默认的编码格式（default encoding）: " + Charset.defaultCharset());
		System.out.println("file encoding: " + System.getProperty("file.encoding"));
		System.out.println("sun.jnu.encoding: " + System.getProperty("sun.jnu.encoding"));
		log.info("更新用户数据：{}", user);
		return "index";
	}

	@DeleteMapping("/user/{id}")
	public String deleteUser(@PathVariable("id") Integer id) {
		System.out.println("删除用户数据");
		return "index";
	}
}
